using FairyGUI;

namespace GameFramework.UI.FairyGUI
{
    public partial class AFairyGUIFormLogic
    {
        /// <summary>
        /// 是否是全屏界面
        /// </summary>
        protected virtual bool IsFullScreen => false;
        
        /// <summary>
        /// 是否是水平居中
        /// </summary>
        protected virtual bool IsHorizontalCenter => false;
        
        /// <summary>
        /// 是否垂直居中
        /// </summary>
        protected virtual bool IsVerticalCenter => false;
        
        private void _OnInitScreenAdapter()
        {
            var uiGroupHelper = (FairyGUIGroupHelper)UIForm.UIGroup.Helper;
            var uiGroupRoot = uiGroupHelper.GroupRoot;

            if (IsFullScreen)
            {
                ContentPane.size = uiGroupRoot.size;
                ContentPane.AddRelation(uiGroupRoot, RelationType.Size);
            }

            var xy = ContentPane.xy;

            if (IsHorizontalCenter)
                xy.x = (uiGroupRoot.width - ContentPane.width) / 2;
            
            if (IsVerticalCenter)
                xy.y = (uiGroupRoot.height - ContentPane.height) / 2;
            
            ContentPane.SetXY(xy.x, xy.y, true);
            
            if (IsHorizontalCenter)
                ContentPane.AddRelation(uiGroupRoot, RelationType.Center_Center);
            
            if (IsVerticalCenter)
                ContentPane.AddRelation(uiGroupRoot, RelationType.Middle_Middle);
        }
        
        private void _OnRecycleScreenAdapter()
        {
            var uiGroupHelper = (FairyGUIGroupHelper)UIForm.UIGroup.Helper;
            var uiGroupRoot = uiGroupHelper.GroupRoot;
            
            if (IsFullScreen)
                ContentPane.RemoveRelation(uiGroupRoot, RelationType.Size);

            if (IsHorizontalCenter)
                ContentPane.RemoveRelation(uiGroupRoot, RelationType.Center_Center);
            
            if (IsVerticalCenter)
                ContentPane.RemoveRelation(uiGroupRoot, RelationType.Middle_Middle);
        }
    }
}